package com.eclink.asset.controller;

import com.eclink.asset.service.AssetCategoryService;
import com.eclink.asset.warehouse.service.WarehouseService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.eclink.asset.domain.dto.DistributeItem;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.dto.query.RecordMenuQueryDTO;
import com.ruoyi.domain.entity.RecordMenuVisit;
import com.ruoyi.web.service.IndexService;
import com.ruoyi.web.service.RecordMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 首页的相关接口
 *
 * @author chenshixian
 */
@Api(tags="首页的相关接口")
@RestController
@RequestMapping("index")
public class IndexController {

    @Resource
    private IndexService indexService;

    @Resource
    private RecordMenuService recordMenuService;

    @Resource
    private AssetCategoryService assetCategoryService;

    @Resource
    private WarehouseService warehouseService;

    /**
     * 资产种类的分布
     * @return 结果
     */
    @ApiOperation("统计资产种类的分布")
    @GetMapping("chart/assetCategoryDistribute")
    public AjaxResult chartAssetCategoryDistribute() {
        List<DistributeItem> list = indexService.getAssetCategoryDistribute();
        for (DistributeItem item : list) {
            item.setName(assetCategoryService.selectNameById(item.getName()));
        }
        return AjaxResult.success(list);
    }

    /**
     * 资产仓库的分布
     * @return 结果
     */
    @ApiOperation("统计资产仓库的分布")
    @GetMapping("chart/assetWarehouseDistribute")
    public AjaxResult chartAssetWarehouseDistribute() {
        List<DistributeItem> list = indexService.getAssetWarehouseDistribute();
        for (DistributeItem item : list) {
            String name = item.getName();
            if (StringUtils.isNotBlank(name)) {
                item.setName(warehouseService.getWarehouseNameById(Long.parseLong(name)));
            } else {
                item.setName("未知");
            }

        }
        return AjaxResult.success(list);
    }

    /**
     * 耗材的领用情况
     * @return 结果
     */
    @ApiOperation("统计耗材的领用情况")
    @GetMapping("chart/consume/use")
    public AjaxResult chartConsumeUseStatistics() {
        List<DistributeItem> list = indexService.getConsumeUseStatistics();
        return AjaxResult.success(list);
    }

    /**
     * 耗材的入库的情况
     * @return 结果
     */
    @ApiOperation("统计耗材入库的情况")
    @GetMapping("chart/consume/in")
    public AjaxResult chartConsumeIn() {
        List<DistributeItem> list = indexService.getConsumeInStatistics();
        return AjaxResult.success(list);
    }


    /**
     * 记录菜单的访问
     * @return 操作结果
     */
    @PostMapping("recordMenu")
    public AjaxResult recordMenu(@RequestBody RecordMenuQueryDTO query)
    {
        recordMenuService.recordMenuVisit(query);
        return AjaxResult.success(1);
    }

    /**
     * 获取菜单的访问记录
     * @return 操作结果
     */
    @GetMapping("getMenuRecord")
    public AjaxResult getMenuRecord()
    {

        Long userId = SecurityUtils.getUserId();
        List<RecordMenuVisit> res = recordMenuService.getMenuRecord(userId);
        return AjaxResult.success(res);
    }


}
